import { SkillLearnType } from "../../config/SkillConfig";
import PlayerLearnedSingleSkill from "../../skill/playerheroskillmanager/PlayerLearnedSingleSkill";


type TypeSkillUIInfo={
  curTypeSkillNum:number,
  parentFrame:number //类型技能的父类  方便去切换时候隐藏显示用
}
type SingleSkillUIRelInfo={
  lvlFrame:number //技能等级frame
  skillIconFrame:number  //技能图标frame 方便定位
}


export type PlayerUnitSkillUIInfo={
  playerIndex:number, //玩家单位的UI索引 (一个玩家只需要1个基础UI 多单位公用一个)
  skillUIMainFrame:number, //主体技能UI的句柄
  historyShowTypeSkillFrame?:number,  //上次显示的类别技能UI框体句柄
  historyTipsFrame?:number,   //上一次滑动到技能frame弹出的提示框的句柄
  typeSkillInfo:Map<SkillLearnType,TypeSkillUIInfo> 
  skillDtlMap:Map<string,SingleSkillUIRelInfo> //技能ID和技能配置的映射表


}

 // 遍历联合类型的每个成员
 type IteratedSkillLearnTypes = {
  [K in SkillLearnType]: `${K}`;
}

const learnTypesMap:IteratedSkillLearnTypes={
  Q:'Q',
  W:'W',
  E:'E',
  R:'R',
  D:'D',
  F:'F'
}





/**  */
export default class SkillUICreator {
    
    static playerSkillUIInfoMap:Map<number,PlayerUnitSkillUIInfo> = new Map<number,PlayerUnitSkillUIInfo>();
    //技能UI框体的总大小
    static skillUIWidth=0.65
    static skillUIHeight=0.45
    
 
    // public static openSkillUI(playerIndex:number,unitSkillAttrData:any){
    //     let playerUnitSkillUIInfo=SkillUICreator.playerSkillUIInfoMap.get(playerIndex)
    //    //找到已经创建的UI面板  如果存在
    //     if(playerUnitSkillUIInfo==undefined){
    //         playerUnitSkillUIInfo={playerIndex,skillUIFrame:0}
    //         //创建UI
    //         let skillUIFrame=SkillUICreator.createSkillBaseUI(playerUnitSkillUIInfo)
    //         playerUnitSkillUIInfo.skillUIFrame=skillUIFrame
    //         SkillUICreator.playerSkillUIInfoMap.set(playerIndex,playerUnitSkillUIInfo)
    //     }else{
    //       //关闭UI
    //       DzFrameShow( playerUnitSkillUIInfo.skillUIFrame!, false )
    //     }
    //    //刷新面板信息
        
    // }

    /**
     * 外部逻辑自行控制是否重复的新增问题 避免重复创建
     * @param playerIndex 
     * @param skillInfo 
     */
    public static uiAddPlayerLenardSkill(playerIndex:number,skillConfig:PlayerLearnedSingleSkill){
        //获取下一个类别技能UI信息  一定是初始化完成的情况
        let playerSkillUIInfo=SkillUICreator.playerSkillUIInfoMap?.get(playerIndex);
        if(playerSkillUIInfo==undefined){
          playerSkillUIInfo=SkillUICreator.createSkillBaseUI(playerIndex)
          SkillUICreator.playerSkillUIInfoMap.set(playerIndex,playerSkillUIInfo)
        }
        //找到已经创建好的类别技能UI框体  在此基础上进行技能添加
        let typeSkillUIInfo=playerSkillUIInfo.typeSkillInfo.get(skillConfig.skllLType)
        if(typeSkillUIInfo==undefined){
          console.log('[ERROR]获取玩家类别技能UI信息失败!')
          return
        }
        //绘制frame  根据技能位置推断出新技能的坐标
        let singleSkillIconSize=0.035
        let singleSkillIconPadding=0.01
        let singleSkillIconTextWidth=0.08
        let singleSkillHeihgt=singleSkillIconSize+singleSkillIconPadding   //icon+text+padding    0.035+0.03+0.02
        let rowNum=parseInt((SkillUICreator.skillUIHeight-singleSkillIconPadding*6)/singleSkillHeihgt+"")-1  //一行放几个
        console.log("rowNum:", rowNum)
        let px=0;
        let py=0;
        let firstRowTopPadding=0.07     //第一行的高度
        let firstColumnPadding=0.04
        typeSkillUIInfo.curTypeSkillNum=typeSkillUIInfo.curTypeSkillNum+1
        let curIndex=typeSkillUIInfo.curTypeSkillNum-1   //从0开始算
        py=((curIndex%rowNum-2)*singleSkillHeihgt*-1)-0.05   //-2就是修正初始位置
        px= parseInt(curIndex/rowNum+"")* (singleSkillIconPadding*4+singleSkillIconTextWidth)
        // if(typeSkillUIInfo.curTypeSkillNum%rowNum>rowNum){
        //    py=0
        // }else{
        //    py=py-singleSkillHeihgt*typeSkillUIInfo.curTypeSkillNum   //0.8是frame总宽 800*600分辨率
        // }
        // if(typeSkillUIInfo.curTypeSkillNum!=1 && typeSkillUIInfo.curTypeSkillNum%rowNum===0){
        //    px=px+(singleSkillIconPadding*4+SkillUICreator.skillUIWidth)*(typeSkillUIInfo.curTypeSkillNum/rowNum)
        // }
        


            let tx=px+firstColumnPadding   //目标x位置  技能块相对偏移+ 左padding
            console.log("??创建技能UI[",skillConfig.skillName,"]索引号[", curIndex,"],tx:",tx," ,py:",py)

            // let imgiframe = DzCreateFrame("QuestButtonBaseTemplate", boxFrame, 0) //DzCreateFrameByTagName("BACKDROP","", backgroundIframe,"Mbackdorp", 0)
            //  DzFrameSetSize( imgiframe, 0.06, 0.06 )
            //  //DzFrameSetTexture( imgiframe,(skillConfig.learnIconPath || "ReplaceableTextures\\CommandButtons\\BTNSelectHeroOn.blp") , 0 )
            //  DzFrameShow( imgiframe, true )
            //  DzFrameSetPoint( imgiframe, 0, boxFrame, 0, tx, py-topPadding )

             let iconFrame=DzCreateFrameByTagName("BACKDROP","", typeSkillUIInfo!.parentFrame,"Mbackdorp", 0)
             let iconPath=skillConfig.skillInconPath || "ReplaceableTextures\\CommandButtons\\BTNSelectHeroOn.blp"
             let skillNameText=skillConfig.skillName
             let skillLvlText="lv："+skillConfig.skillLvl
             if(typeSkillUIInfo.curTypeSkillNum>2){
              iconPath=iconPath.replace('CommandButtons\\BTN','CommandButtonsDisabled\\DISBTN')
              skillNameText='|cff8B8989'+skillNameText
              skillLvlText='|cff8B8989'+skillLvlText
             }
             
             DzFrameSetTexture( iconFrame, iconPath, 0 )
             DzFrameSetSize( iconFrame, singleSkillIconSize, singleSkillIconSize )
             DzFrameShow( iconFrame, true )
             DzFrameSetPoint( iconFrame, 0, typeSkillUIInfo!.parentFrame, 0, tx, py-firstRowTopPadding )

             //文字描述
             let  skillNameTextFrame = DzCreateFrameByTagName("TEXT","", iconFrame,"", 0)  //CustomText    CustomTextSmall
             DzFrameSetText( skillNameTextFrame,skillNameText  )
             DzFrameSetSize( skillNameTextFrame, singleSkillIconTextWidth, singleSkillHeihgt)
             //DzFrameShow( skillDescTextFrame, true )
             //DzFrameSetTextAlignment(titleTextFrame,1)
             DzFrameSetPoint( skillNameTextFrame, 3, iconFrame, 5,0.01, -0.01 ) 

             
             
             //技能框体的tips
             DzFrameSetScriptByCode(skillNameTextFrame,2,
               function(){
                   console.log("===============tips open======= ",skillConfig.skillName)
                   if(playerSkillUIInfo?.historyTipsFrame!=undefined){
                     //上次的技能tips还存在 直接销毁掉
                     DzDestroyFrame(playerSkillUIInfo?.historyTipsFrame || 0)
                   }

                   playerSkillUIInfo!.historyTipsFrame=SkillUICreator.createSkillTipsUI(skillConfig.skillDesc || '未定义',typeSkillUIInfo!.parentFrame || 0,tx+0.1,py-firstRowTopPadding)
    
               }
               ,false)
               DzFrameSetScriptByCode(skillNameTextFrame,3,
                  function(){
                      console.log("===============tips close======= ",skillConfig.skillName)
                      if(playerSkillUIInfo?.historyTipsFrame!=undefined){
                        //本次or上次的技能tips还存在 直接销毁掉
                        DzDestroyFrame(playerSkillUIInfo?.historyTipsFrame || 0)
                        playerSkillUIInfo!.historyTipsFrame=undefined
                      }  
                  }
                  ,false) 

             //等级描述
            let  skillLvlTextFrame = DzCreateFrameByTagName("TEXT","", iconFrame,"", 0)  //CustomText    CustomTextSmall
            DzFrameSetText( skillLvlTextFrame,skillLvlText  )
            DzFrameSetSize( skillLvlTextFrame, singleSkillIconTextWidth, singleSkillHeihgt)
            DzFrameSetPoint( skillLvlTextFrame, 3, iconFrame, 5,0.01, -0.02 ) 

          //添加新技能UI关系 方便后续跟踪
          playerSkillUIInfo?.skillDtlMap.set(skillConfig.skillId,{'lvlFrame':skillLvlTextFrame,'skillIconFrame':iconFrame})          //TODO 这里需要修改lvlFrame
    }


    /**
     * 同步技能等级  外部逻辑自行控制是否重复的新增问题 
     * @param playerIndex 
     * @param skillInfo 
     */
      public static uiSyncPlayerLenardSkillLvl(playerIndex:number,skillConfig:PlayerLearnedSingleSkill){
        //找到已经创建好的类别技能UI框体  
        let singleSkillUIInfo=SkillUICreator.playerSkillUIInfoMap?.get(playerIndex)?.skillDtlMap.get(skillConfig.skillId)
        if(singleSkillUIInfo==undefined){
          console.log('[ERROR]获取玩家类别技能UI信息失败 rel by uiSyncPlayerLenardSkillLvl!')
          return
        }
        DzFrameSetText(  singleSkillUIInfo.lvlFrame,"lv："+skillConfig.skillLvl)
      }

   
      

    private  static createSkillBaseUI(playerIndex:number):PlayerUnitSkillUIInfo{
        let gameUI=DzGetGameUI()

        DzLoadToc("ui\\custom.toc")
        //创建基础UI

   
        // let backgroundIframe=DzCreateFrameByTagName("BACKDROP","",gameUI,"TextBackDrop",0)
        // DzFrameSetSize( backgroundIframe, 0.75, 0.25 )
        // DzFrameShow( backgroundIframe, true )
        // DzFrameSetPoint( backgroundIframe, 4, gameUI, 4, 0, 0 )  


      //create a hidden Frame a container for all
      let windowcontainerFrame = DzCreateFrameByTagName("FRAME", "", gameUI, "", 0)
      //create a box as child of the container
      let boxFrame = DzCreateFrameByTagName("BACKDROP", "", windowcontainerFrame, "EscMenuBackdrop", 0)    
      DzFrameSetSize(boxFrame, SkillUICreator.skillUIWidth,SkillUICreator.skillUIHeight )
      DzFrameSetAbsolutePoint(boxFrame, 4, 0.4, 0.34)

      
      // 窗口关闭X按钮
      let closeButton = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)
      DzFrameSetSize(closeButton, 0.03, 0.03)
      DzFrameSetText(closeButton, "X")
      DzFrameSetPoint(closeButton, 2, boxFrame, 2, 0, 0)
      DzFrameSetScriptByCode(closeButton,1,function(){
        //if ( GetLocalPlayer() == GetTriggerPlayer() ){
          DzFrameShow( windowcontainerFrame, false )
        //}
      },true)


      // create 3 Buttons based on ScriptDialogButton
      let buttonPageQ = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)
      let buttonPageW = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)
      let buttonPageE = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)  
      let buttonPageR = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)     
      let buttonPageT = DzCreateFrameByTagName("GLUETEXTBUTTON", "", boxFrame, "ScriptDialogButton", 0)     
      
      DzFrameSetSize(buttonPageQ, 0.08, 0.03)
      DzFrameSetSize(buttonPageW, 0.08, 0.03)
      DzFrameSetSize(buttonPageE, 0.08, 0.03)
      DzFrameSetSize(buttonPageR, 0.08, 0.03)
      DzFrameSetSize(buttonPageT, 0.08, 0.03)
  
      DzFrameSetText(buttonPageQ, "伤害")
      DzFrameSetText(buttonPageW, "辅助")
      DzFrameSetText(buttonPageE, "机动")
      DzFrameSetText(buttonPageR, "爆发")
      DzFrameSetText(buttonPageT, "秘技")

      let topBtnPadding=-0.03
      
      DzFrameSetPoint(buttonPageE, 1, boxFrame, 1, 0, topBtnPadding)
      DzFrameSetPoint(buttonPageW, 5, buttonPageE, 3, 0, 0)   //W的右边挂在E的左边
      DzFrameSetPoint(buttonPageQ, 5, buttonPageW, 3, 0, 0)   //Q的右边挂在W的左边
      DzFrameSetPoint(buttonPageR, 3, buttonPageE, 5, 0, 0)   //R的左边边挂在E的右边    
      DzFrameSetPoint(buttonPageT, 3, buttonPageR, 5, 0, 0)   //T的左边边挂在R的右边    


      //为每一种技能类型都创建一个技能类型框体 作为后续技能模组父框体
      let allTypeSkillMap=new Map<SkillLearnType,TypeSkillUIInfo>()
      //最终返回对象
      let rs:PlayerUnitSkillUIInfo={
        playerIndex:playerIndex,
        skillUIMainFrame:windowcontainerFrame,
        historyTipsFrame:undefined,   
        historyShowTypeSkillFrame:undefined,
        typeSkillInfo:allTypeSkillMap,
        skillDtlMap:new Map(),
      }
      
      for(let k in learnTypesMap){
        let typeSkillParentFrame= DzCreateFrameByTagName("FRAME", "", boxFrame, "", 0)
        //create a box as child of the container
        DzFrameSetSize(typeSkillParentFrame, SkillUICreator.skillUIWidth,SkillUICreator.skillUIHeight+topBtnPadding )
       // DzFrameSetSize(typeSkillParentFrame, 0.4, 0.3+topBtnPadding)
        DzFrameSetPoint( typeSkillParentFrame, 0, boxFrame, 0, 0, topBtnPadding*2 )  //把上方按钮留出来
        DzFrameShow( typeSkillParentFrame, k=='Q' )
        let typeSkillUIInfo:TypeSkillUIInfo={curTypeSkillNum:0,parentFrame:typeSkillParentFrame}
        allTypeSkillMap.set(k as SkillLearnType,typeSkillUIInfo)

        let bindBtn=undefined
        switch(k){
          case 'Q':
            bindBtn=buttonPageQ;
            rs.historyShowTypeSkillFrame=typeSkillParentFrame //默认显示Q类别窗口UI
            break;
          case 'W':  
            bindBtn=buttonPageW;
           break;
          case 'E':
            bindBtn=buttonPageE;
            break;
          case 'R':
            bindBtn=buttonPageR;
            break;  
          case 'T':  
            bindBtn=buttonPageT;
            break;  
          default:
            console.log("[ERROR]玩家技能基础数据异常!",k)
            break;
        }
        DzFrameSetScriptByCode(bindBtn!,1,function(){
          console.log('点击技能类别：',k)
          //if ( GetLocalPlayer() == GetTriggerPlayer() ){
          if(rs.historyShowTypeSkillFrame!=undefined){
            //DzFrameSetFocus( bindBtn!, true )
            DzFrameShow( rs.historyShowTypeSkillFrame, false )
          }
          rs.historyShowTypeSkillFrame=typeSkillParentFrame
          DzFrameShow( typeSkillParentFrame, true )  //TODO
         // DzFrameSetFocus( bindBtn!, true )
         // }
        },false)
      }

        return rs  
    }


    public static createSkillTipsUI(content:string,parentUI:number,px:number,py:number):number{

      let tipsBackgroundIframe=DzCreateFrameByTagName("BACKDROP","",parentUI,"TextBackDrop",0)
      DzFrameSetSize( tipsBackgroundIframe, 0.15, 0.15 )
      DzFrameShow( tipsBackgroundIframe, true )
      //DzFrameSetPoint( backgroundIframe, 4, DzGetGameUI(), 4, -0.24, -0.05 )  
      DzFrameSetPoint( tipsBackgroundIframe, 0, parentUI, 0, px, py )  

      let  descTextFrame = DzCreateFrameByTagName("TEXT","", tipsBackgroundIframe,"", 0)
      DzFrameSetText( descTextFrame, content )
      DzFrameSetSize( descTextFrame, 0.15, 0.15 )
      DzFrameShow( descTextFrame, true )
      //DzFrameSetTextAlignment(descTextFrame,2)
      //DzFrameSetPoint( descTextFrame, 4, tipsBackgroundIframe, 4,0, -0.04 ) 

      DzFrameSetPoint( descTextFrame, 0, tipsBackgroundIframe, 0,0.005, -0.02 ) 

      return tipsBackgroundIframe
    }



  
}  